<template>
	<div class="yellow" style="position: fixed; left: 0; top: 0; z-index: 999; width: 100%; height: 100%;">
		<div class="header flex a-center jlr">
			<div class="back" style="box-sizing: content-box; padding-right:0.7rem;" @click="back"><i class="iconfont icon-arrow-left-copy"></i></div>
			<h2>地址管理</h2>
			<div class="back" style="width: 1.56rem;" @click="add">新增地址</div>
		</div>

		<div class="abox">
			<van-swipe-cell :right-width="70" :left-width="70" v-for="(v,i) in addrList" :key="v.addrId">
				<div class="addressItem flex a-center">
					<div class="fx1" @click="isselect(v)">
						<div class="title flex a-center">
							<h3>{{v.contactsName}}</h3><span>{{v.phone}}</span>
						</div>
						<div class="addressmsg">
							<b v-if="v.isDefault">默认</b>
							{{v.dtlAddr}}
						</div>
					</div>
					<div class="edit" @click="edit(v)">编辑</div>
				</div>
				<span slot="right">
					<div class="removeBtn" @click="remove(v.addrId)">删除</div>
				</span>
			</van-swipe-cell>
		</div>
		<addedit :show.sync="show" v-if="show" :params="obj" @cb="cb"></addedit>
	</div>
</template>
<script>
	import addedit from './addedit.vue'
	export default {
		components: {
			addedit
		},
		props: {
			shows: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				obj: null,
				show: false,
				addrList: []
			}
		},
		created() {
			this.init()
		},
		methods: {
			init() {
				this.obj = null;
				this.$ajax.getAddrList().then(res => {
					console.log('地址', res)
					if (res.code == 0) {
						this.addrList = res.data.addrList
					}
				})
			},
			isselect(v) {
				if (this.shows) {
					this.$emit('update:shows', false)
					this.$emit('cb', v)
				}
			},
			add() {
				this.obj = null
				this.show = true
			},
			edit(v) {
				const reqData = {
					addrId: v.addrId
				};
				this.$ajax.getAddrById(reqData).then(res => {
					if (res.code == 0) {
						this.obj = res.data;
						this.show = true
					}
				})
			},
			remove(id) {
				var that = this
				that.$dialog.confirm({
					title: '提示',
					message: '是否确认删除？'
				}).then(() => {
					that.$ajax.delAddr({
						addrId: id
					}).then(res => {
						if (res.code == 0) {
							that.$toast.success('删除成功')
							that.init()
						}
					})
				})
			},
			back() {
				console.log(this.shows)
				if(this.shows){
					this.$emit('update:shows',false)
				}else{
					this.obj = null;
					this.show = false;
					this.$router.go(-1);
				}
				
			},
			cb() {
				this.init()
			}
		}
	}
</script>
